#include "datetimepickerdialog.h"
#include "ui_datetimepickerdialog.h"

DateTimePickerDialog::DateTimePickerDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DateTimePickerDialog)
{
    ui->setupUi(this);
    hourPattern    = QString("%1:%2:%3");
    this->dateTime = "";
    ui->calendarWidget->setGridVisible(true);
    ui->groupBox->hide();
    ui->ck_time_show->setChecked(false);
    connect(ui->spinBox_Hours, SIGNAL(valueChanged(int)), ui->hours, SLOT(setValue(int)));
    connect(ui->hours, SIGNAL(valueChanged(int)), ui->spinBox_Hours, SLOT(setValue(int)));

    connect(ui->spinBox_Minutes, SIGNAL(valueChanged(int)), ui->minutes, SLOT(setValue(int)));
    connect(ui->minutes, SIGNAL(valueChanged(int)), ui->spinBox_Minutes, SLOT(setValue(int)));

    connect(ui->spinBox_Second, SIGNAL(valueChanged(int)), ui->Second, SLOT(setValue(int)));
    connect(ui->Second, SIGNAL(valueChanged(int)), ui->spinBox_Second, SLOT(setValue(int)));

    setType();
}

DateTimePickerDialog::~DateTimePickerDialog()
{
    delete ui;
}

void DateTimePickerDialog::setType(int nType)
{
    if(nType==1){
        ui->ck_time_show->show();
        ui->lcdNumber_datetime->setDigitCount(19);
        ui->lcdNumber_datetime->setFixedWidth(240);
    }else{
        ui->ck_time_show->hide();
        ui->lcdNumber_datetime->setDigitCount(11);
        ui->lcdNumber_datetime->setFixedWidth(140);
    }
}

QString DateTimePickerDialog::GetDateTime()
{
    return dateTime;
}


void DateTimePickerDialog::on_hours_sliderMoved(int position)
{
    this->time     = QString(this->hourPattern).arg(position,2,10,QChar('0')).arg(ui->minutes->value(),2,10,QChar('0')).arg(ui->Second->value(),2,10,QChar('0'));
    this->dateTime = QString("%1 %2").arg(this->date).arg(this->time);
    ui->lcdNumber_datetime->display(dateTime);
}

void DateTimePickerDialog::on_minutes_sliderMoved(int position)
{
    this->time     = QString(this->hourPattern).arg(ui->hours->value(),2,10,QChar('0')).arg(position,2,10,QChar('0')).arg(ui->Second->value(),2,10,QChar('0'));
    this->dateTime = QString("%1 %2").arg(this->date).arg(this->time);
    ui->lcdNumber_datetime->display(dateTime);
}

void DateTimePickerDialog::on_calendarWidget_clicked(const QDate &date)
{
    this->date     = date.toString("yyyy-MM-dd");
    this->dateTime = QString("%1 %2").arg(this->date).arg(this->time);
    ui->lcdNumber_datetime->display(dateTime);
}

void DateTimePickerDialog::on_pushButton_clicked()
{
    this->dateTime = QString("%1 %2").arg(this->date).arg(this->time);
    ui->lcdNumber_datetime->display(dateTime);
    this->close();
}

void DateTimePickerDialog::on_Second_sliderMoved(int position)
{
    this->time = QString(this->hourPattern).arg(ui->hours->value(),2,10,QChar('0')).arg(ui->minutes->value(),2,10,QChar('0')).arg(position,2,10,QChar('0'));
    this->dateTime = QString("%1 %2").arg(this->date).arg(this->time);
    ui->lcdNumber_datetime->display(dateTime);
}

void DateTimePickerDialog::on_btn_Reset_clicked()
{
    this->dateTime = "";
    this->time     = "";
    this->date     = "";
    ui->lcdNumber_datetime->display("");

}

void DateTimePickerDialog::on_btn_today_clicked()
{
    ui->calendarWidget->showToday();
    ui->calendarWidget->setSelectedDate(QDate::currentDate());
    ui->calendarWidget->setFocus();
    this->date     = ui->calendarWidget->selectedDate().toString("yyyy-MM-dd");
    this->dateTime = QString("%1 %2").arg(this->date).arg(this->time);
    ui->lcdNumber_datetime->display(dateTime);
}

void DateTimePickerDialog::on_ck_time_show_clicked()
{
    if(ui->ck_time_show->isChecked()){
        ui->groupBox->show();
    }else{
        ui->groupBox->hide();
    }
}

void DateTimePickerDialog::on_spinBox_Hours_valueChanged(int arg1)
{
    on_hours_sliderMoved(arg1);
}

void DateTimePickerDialog::on_spinBox_Minutes_valueChanged(int arg1)
{
    on_minutes_sliderMoved(arg1);
}

void DateTimePickerDialog::on_spinBox_Second_valueChanged(int arg1)
{
    on_Second_sliderMoved(arg1);
}
